//
//  ZDDMusicManger.m
//  QQ音乐(皇家尊享版)
//
//  Created by 周冬冬 on 16/7/21.
//  Copyright © 2016年 我的mac. All rights reserved.
//

#import "ZDDMusicManger.h"
#import <AVFoundation/AVFoundation.h>
static ZDDMusicManger * instance;
@interface ZDDMusicManger ()

//当前播放的文件名
@property(nonatomic,copy)NSString * currentFilename;

@end
@implementation ZDDMusicManger
+(instancetype)sharedManger{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[ZDDMusicManger alloc] init];
    });


    return instance;



}
//播放/继续播放音乐
-(void)playMusicWithFilename:(NSString *)filename{

    //判断当前歌曲
    if (self.currentFilename != filename) {
        
        //获取路径
        NSString * filePath = [[NSBundle mainBundle] pathForResource:filename ofType:nil];
        //创建播放器
        self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath] error:nil];
        //准备播放
        [self.audioPlayer prepareToPlay];
        //赋值
        self.currentFilename = filename;
    }
    //延迟
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        //开始播放
        [self.audioPlayer play];
    });



}
//zant音乐
-(void)pauseMusic{

    [self.audioPlayer pause];
}
#pragma mark - setter&getter
//获取时长
-(NSTimeInterval)duration{


    return self.audioPlayer.duration;
}
//当前时长
-(NSTimeInterval)currentTime{
    return self.audioPlayer.currentTime;

}
-(void)setCurrentTime:(NSTimeInterval)currentTime{


    //手动播放进度
    self.audioPlayer.currentTime = currentTime;

}
@end
















